const axios = require("axios");
const url = require("url");
const { Router } = require("express");

const router = new Router();

router.get("/home", async (req, res) => {
    const data = await axios.get(
        "https://m.you.163.com/xhr/index.json?__timestamp=" +
            new Date().getTime()
    );

    var listData = data.data.data.data;

    //轮播图数据
    const bannerData = listData.focusList.map((item) => {
        return {
            id: item.id,
            picUrl: item.picUrl,
        };
    });
    //分类数据
    const cateData = listData.kingKongModule.kingKongList
        .filter((item, index) => {
            return index >0 && index <= 8;
        })
        .map((item) => {
            const {
                query: { categoryId },
            } = url.parse(item.schemeUrl, true);
            return {
                picUrl: item.picUrl,
                id: categoryId,
                text: item.text,
            };
        });
    // 新品首发
    const newGoodsData = listData.newItemList.map((item) => ({
        id: item.id,
        name: item.name,
        picUrl: item.primaryPicUrl,
        retailPrice: item.retailPrice,
    }));
    res.json({ bannerData, cateData, newGoodsData });
});
router.get("/cate_list", async (req, res) => {
    const result = await axios.get("http://m.you.163.com/item/list.json", {
        params: {
            __timestamp: new Date().getTime(),
            categoryId: req.query.id,
        },
    });
    const listData = result.data.data.categoryItemList.map(
        ({ category, itemList }) => ({
            category: {
                id: category.id,
                name: category.name,
                frontName: category.frontName,
            },
            itemList: itemList.map((item) => ({
                id: item.id,
                name: item.name,
                listPicUrl: item.listPicUrl,
                simpleDesc: item.simpleDesc,
                listPromBanner: item.listPromBanner,
                retailPrice: item.retailPrice,
                counterPrice: item.counterPrice,
                itemTagList: item.itemTagList,
            })),
        })
    );
    res.json({
        listData,
    });
});


router.get('/detail',async(req,res)=>{
    const id=req.query.id;
    const {data:{item}}=await axios.get('https://m.you.163.com/item/detail?id='+id);
    const bannerData = Object.entries(item.itemDetail).filter(([key])=>key.startsWith('picUrl')).map(([key, value])=>value);
    const videoInfo = item.itemDetail.videoInfo;
    const name = item.name;
    const retailPrice=item.retailPrice;
    const counterPrice=item.counterPrice;
    const detailHtml = item.itemDetail.detailHtml;
    const skuList=item.skuSpecList;
    const promotionDesc=item.promotionDesc;
    const skuMap = item.skuMap;
    const primaryPicUrl = item.primaryPicUrl;
    const ids=item.id;
    res.json({bannerData,retailPrice,primaryPicUrl,skuMap,counterPrice,skuList,promotionDesc, videoInfo, name,id:ids, detailHtml});
})
module.exports = router;
